﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace TM.QWeibo.Client.Message
{
    public partial class Dialog : PhoneApplicationPage
    {
        #region 私有变量

        string _accountName { get; set; }

        private SDK.Objects.UserInfo _user;

        #endregion

        public Dialog()
        {
            InitializeComponent();
            UserInfo.TapAvatar = ViewPersionInfo;
            this.ApplicationBar.BackgroundColor = (Color)App.Current.Resources["ApplicationBarBackgroundColor"];
        }

        /// <summary>
        /// 查看用户资料
        /// </summary>
        /// <param name="name"></param>
        private void ViewPersionInfo(string name)
        {
            if (!string.IsNullOrWhiteSpace(name))
            {
                Deployment.Current.Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri(string.Format("/Profile.xaml?{0}={1}", AppSetting.UserNameParameterKey, name), UriKind.Relative)));
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (NavigationContext.QueryString.ContainsKey(AppSetting.UserNameParameterKey))
            {
                var name = NavigationContext.QueryString[AppSetting.UserNameParameterKey];
                if (!string.IsNullOrWhiteSpace(name))
                {
                    _accountName = name;
                    progressBar.IsIndeterminate = true;
                    GetUserInfo();
                    DialogMessageList.Init(name, BeforeLoading, AfterLoaded);
                }
            }
        }

        /// <summary>
        ///  请求用户数据
        /// </summary>
        private void GetUserInfo()
        {
            TM.QWeibo.Dal.User.Instance.GetBriefInfos(_accountName, GetUserInfoEnd);
        }

        /// <summary>
        /// 请求用户资料回调
        /// </summary>
        /// <param name="userInfo"></param>
        private void GetUserInfoEnd(TM.QWeibo.SDK.Objects.UserInfo userInfo)
        {
            Dispatcher.BeginInvoke(() =>
            {
                _user = userInfo;
                UserInfo.BindInfo(userInfo);
                progressBar.IsIndeterminate = false;
            });
        }

        /// <summary>
        /// 加载数据前显示loading状态
        /// </summary>
        private void BeforeLoading()
        {
            Dispatcher.BeginInvoke(() => progressBar.IsIndeterminate = true);
        }
        /// <summary>
        /// 加载数据后隐藏loading状态，显示相关信息
        /// </summary>
        private void AfterLoaded(object count)
        {
            Dispatcher.BeginInvoke(() =>
            {
                progressBar.IsIndeterminate = false;
            });
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri(string.Format("/Tweet/Add.xaml?{0}={1}&{2}={3}", AppSetting.AddTweetTypeParameterKey, Helper.EAddTweetType.PrivateMessage.GetHashCode(), AppSetting.AddTweetMetaParameterKey, _user.Name), UriKind.Relative));
        }
    }
}